package javase2.day03;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 
 * RandomAccessFile Demo
 *
 */
public class Demo1 {
	public static void main(String[] args) throws IOException {
		String file = "raf.dat";
		RandomAccessFile raf = new RandomAccessFile(file, "rw");
		System.out.println(raf.getFilePointer()); //0
		raf.write(0x41);
		System.out.println(raf.getFilePointer()); //1
		raf.write(66);  //0x42
		raf.write(67);
		int i = 88;
		int d1=i>>>24 & 0xff;
		int d2=i>>>16 & 0xff;
		int d3=i>>>8 & 0xff;
		int d4=i & 0xff;
		raf.write(d1);
		raf.write(d2);
		raf.write(d3);
		raf.write(d4);
		byte[] buf = "中国".getBytes("GBK");
		raf.write(buf);
		System.out.println(raf.getFilePointer());
		raf.write(buf,2,2); //国
		System.out.println(raf.length());
		
		raf.seek(0); //seek检索，移动文件指针
		int b =raf.read(); //41--> 00 00 00 41 ->b
		System.out.println(Integer.toBinaryString(b)); 
		System.out.println(Integer.toHexString(b)); //41
		System.out.println(raf.getFilePointer()); //1
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(b);
		b=raf.read();
		System.out.println(Integer.toHexString(b));
		
		raf.close();
	}
}
